package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.ProjectBean;
import com.woniuxy.educationplatform.bean.ResultMsg;
import com.woniuxy.educationplatform.bean.SpecialBean;
import com.woniuxy.educationplatform.service.IProjectService;
import com.woniuxy.educationplatform.service.ISpecialService;
import com.woniuxy.educationplatform.util.OrderUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/special")
public class SpecialController {
    @Resource
    private ISpecialService specialServiceImpl;
    @Resource
    private IProjectService projectServiceImpl;

    /**
     * 查询所有专栏
     *
     * @return
     */
    @RequestMapping(value = "/page", produces = {"application/json;charset=utf-8"})
    public ResultMsg<SpecialBean> findAll() {
        List<SpecialBean> all = specialServiceImpl.findAll();
        for (SpecialBean s : all) {
            ProjectBean projectBean = projectServiceImpl.findById(Long.valueOf(s.getFkProjectId()));
            System.out.println(Long.valueOf(s.getFkProjectId()));
            System.out.println(projectBean);
            s.setProjName(projectBean.getProjName());
            System.out.println(s);
        }

        return new ResultMsg(200, "查询成功！", all);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "del")
    public ResultMsg updateDel(Integer id) {
        System.out.println(id);
        int del = specialServiceImpl.del(id);
        System.out.println(del + "删除成功");
        ResultMsg resultMsg = new ResultMsg(200, "删除成功", null);
        return resultMsg;
    }

    /**
     * 上架
     *
     * @param id
     * @return
     */
    @GetMapping(value = "putColumn")
    public ResultMsg putColumn(Integer id) {
        System.out.println(id);
        int del = specialServiceImpl.putaway(id);
        System.out.println(del + "上架成功");
        ResultMsg resultMsg = new ResultMsg(200, "上架成功", null);
        return resultMsg;
    }

    /**
     * 上架
     *
     * @param id
     * @return
     */
    @GetMapping(value = "downColumn")
    public ResultMsg downColumn(Integer id) {
        System.out.println(id);
        int del = specialServiceImpl.updateStatus(id);
        System.out.println(del + "上架成功");
        ResultMsg resultMsg = new ResultMsg(200, "下架成功", null);
        return resultMsg;
    }

    /**
     * 模糊查询专栏
     *
     * @param specialBean
     * @return
     * @throws ParseException
     */
    @GetMapping(value = "findLike")
    public ResultMsg findEntityParams(SpecialBean specialBean) {
        System.out.println(specialBean);
        List<SpecialBean> allByParams = specialServiceImpl.findAllByParams(specialBean);
        System.out.println(allByParams);
        ResultMsg resultMsg = new ResultMsg(200, "模糊查询", allByParams);
        return resultMsg;
    }

    /**
     * 修改专栏
     *
     * @param specialBean
     * @return
     * @throws ParseException
     */
    @GetMapping(value = "upColumn")
    public ResultMsg upColumn(SpecialBean specialBean) {
        System.out.println(specialBean);
        int i = specialServiceImpl.updateSpecial(specialBean);
        System.out.println(i);
        ResultMsg resultMsg = new ResultMsg(200, "修改成功", null);
        return resultMsg;
    }

    /**
     * 查询项目分类
     *
     * @param projectBean
     * @return
     */
    @GetMapping(value = "getPro")
    public ResultMsg getPro(ProjectBean projectBean) {
        System.out.println(projectBean);
        List<ProjectBean> projectBeans = projectServiceImpl.findAll();
        for (ProjectBean p : projectBeans
        ) {
            System.out.println(p);
        }
        ResultMsg resultMsg = new ResultMsg(200, "修改成功", projectBeans);
        return resultMsg;
    }

    /**
     * 保存专栏
     *
     * @param specialBean
     * @return
     */
    @GetMapping(value = "addCol")
    public ResultMsg addCol(SpecialBean specialBean) {
        specialBean.setSpecialNo(OrderUtil.generateOrderNo());
        long videoNum = (long) (Math.random() * 29 + 1);
        specialBean.setVideoNum(videoNum);
        long counts = (long) ((Math.random() * 9 + 1) * 1000);
        specialBean.setViewCounts(counts);
        System.out.println(specialBean);
        int i = specialServiceImpl.saveColumn(specialBean);
        ResultMsg resultMsg = new ResultMsg(200, "创建成功", i);
        return resultMsg;
    }


}
